package com.quanyan.place.web;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.common.utils.ObjectUtil;
import com.quanyan.common.utils.UUIDUtils;
import com.quanyan.place.component.MerchantClientPayType;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.TbBizPlaceOrder;
import com.quanyan.place.entity.TbBizPlaceOrderHistory;
import com.quanyan.place.entity.TbBizPlaceOrderItem;
import com.quanyan.place.entity.apireq.ReqPlaceGoodsVo;
import com.quanyan.place.entity.apireq.ReqShoppingCart;
import com.quanyan.place.entity.vo.PlaceScanOrderPayVo;
import com.quanyan.place.entity.vo.TestUpdateUuidVo;
import com.quanyan.place.mapper.TbBizPlaceOrderHistoryMapper;
import com.quanyan.place.mapper.TbBizPlaceOrderItemMapper;
import com.quanyan.place.mapper.UuidMapper;
import com.quanyan.place.service.PlaceMemberService;
import com.quanyan.place.service.PlaceOrderService;
import com.quanyan.place.service.PlaceSaleService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * PlaceSaleController
 * 场馆销售管理
 * @author Li Shuai
 * @date 16/4/14
 */

@RestController
//@RequestMapping("/placeSale")
@RequestMapping("/admin/api/place/sale")
public class PlaceSaleController {
    @Autowired
    PlaceSaleService placeSaleService;
    @Autowired
    PlaceOrderService placeOrderService;
    @Autowired
    PlaceMemberService placeMemberService;
    @Autowired
    UuidMapper uuidMapper;
    @Autowired
    TbBizPlaceOrderHistoryMapper tbBizPlaceOrderHistoryMapper;
    @Autowired
    TbBizPlaceOrderItemMapper tbBizPlaceOrderItemMapper;
    private Logger logger = LoggerFactory.getLogger(PlaceSaleController.class);

    /**
     * 场地销售明细
     * @param map
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/getUnitSaleDetail",method = RequestMethod.POST)
    public APIResponse<?> getUnitSaleDetail(@RequestBody @Valid Map<String, Object> map,
                                            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        /*
        Integer placeId = MapUtils.getInteger(map, "placeId");
        Integer categoryId = MapUtils.getInteger(map, "categoryId");
        Integer orderChannel = MapUtils.getInteger(map, "orderChannel");
        Date startConsumeTime = (Date)MapUtils.getObject(map,"startConsumeTime");
        Date endConsumeTime = (Date)MapUtils.getObject(map,"endConsumeTime");
        String placeUnitName = MapUtils.getString(map,"placeUnitName");
        String placeOrderId = MapUtils.getString(map,"placeOrderId");
        String customerName = MapUtils.getString(map,"customerName");
        String mobile = MapUtils.getString(map,"mobile");
        String innerCardNo = MapUtils.getString(map,"innerCardNo");
        */
        return placeSaleService.getUnitSaleDetail(map);
    }

    /**
     * 场地销售明细导出
     * @param map
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/getUnitSaleDetailExport",method = RequestMethod.POST)
    public APIResponse<?> getUnitSaleDetailExport(@RequestBody @Valid Map<String, Object> map,
                                            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        map.put("pageNum",null);
        map.put("pageSize",null);
        return placeSaleService.getUnitSaleDetailExport(map);
    }

    /**
     * 场地销售统计   --场地编号
     * @param map
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/getUnitSaleStatisticsByPlaceCode",method = RequestMethod.POST)
    public APIResponse<?> getUnitSaleStatisticsByPlaceCode(@RequestBody @Valid Map<String, Object> map,
                                            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return placeSaleService.getUnitSaleStatisticsByPlaceCode(map);
    }

    /**
     * 场地销售统计   --场地编号(导出)
     * @param map
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/getUnitSaleStatisticsByPlaceCodeExport",method = RequestMethod.POST)
    public APIResponse<?> getUnitSaleStatisticsByPlaceCodeExport(@RequestBody @Valid Map<String, Object> map,
                                                           BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        map.put("pageNum",null);
        map.put("pageSize",null);
        return placeSaleService.getUnitSaleStatisticsByPlaceCodeExport(map);
    }

    /**
     * 场地销售统计   --销售渠道
     * @param map
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/getUnitSaleStatisticsByOrderChannel",method = RequestMethod.POST)
    public APIResponse<?> getUnitSaleStatisticsByOrderChannel(@RequestBody @Valid Map<String, Object> map,
                                                           BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return placeSaleService.getUnitSaleStatisticsByOrderChannel(map);
    }

    /**
     * 场地销售统计   --销售渠道(导出)
     * @param map
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/getUnitSaleStatisticsByOrderChannelExport",method = RequestMethod.POST)
    public APIResponse<?> getUnitSaleStatisticsByOrderChannelExport(@RequestBody @Valid Map<String, Object> map,
                                                              BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return placeSaleService.getUnitSaleStatisticsByOrderChannel(map);
    }

    /**
     * 场地销售统计   --运动项目
     * @param map
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/getUnitSaleStatisticsByCategory",method = RequestMethod.POST)
    public APIResponse<?> getUnitSaleStatisticsByCategory(@RequestBody @Valid Map<String, Object> map,
                                                              BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return placeSaleService.getUnitSaleStatisticsByCategory(map);
    }

    /**
     * 场地销售统计   --运动项目(导出)
     * @param map
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/getUnitSaleStatisticsByCategoryExport",method = RequestMethod.POST)
    public APIResponse<?> getUnitSaleStatisticsByCategoryExport(@RequestBody @Valid Map<String, Object> map,
                                                          BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return placeSaleService.getUnitSaleStatisticsByCategory(map);
    }

    /**
     * 场地销售统计   --散客会员
     * @param map
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/getUnitSaleStatisticsByMember",method = RequestMethod.POST)
    public APIResponse<?> getUnitSaleStatisticsByMember(@RequestBody @Valid Map<String, Object> map,
                                                              BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return placeSaleService.getUnitSaleStatisticsByMember(map);
    }

    /**
     * 场地销售统计   --散客会员(导出)
     * @param map
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/getUnitSaleStatisticsByMemberExport",method = RequestMethod.POST)
    public APIResponse<?> getUnitSaleStatisticsByMemberExport(@RequestBody @Valid Map<String, Object> map,
                                                        BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return placeSaleService.getUnitSaleStatisticsByMember(map);
    }


    /**
     * 场地销售tab排序获取
     */
    @RequestMapping(value = "/getSaleTab",method = RequestMethod.POST)
    public APIResponse<?> getSaleTab(@RequestBody @Valid Map<String, Object> map,
                                                  BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer placeId = MapUtils.getInteger(map, "placeId");
        return placeSaleService.getSaleTab(placeId);
    }

    /**
     * 场地销售tab排序设置
     */
    @RequestMapping(value = "/setSaleTab",method = RequestMethod.POST)
    public APIResponse<?> setSaleTab(@RequestBody @Valid Map<String, Object> map,
                                     BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer placeId = MapUtils.getInteger(map, "placeId");
        String saleTab = MapUtils.getString(map,"saleTab");
        return placeSaleService.setSaleTab(placeId,saleTab);
    }

    /**
     * 商家端购物车
     * @param reqShoppingCart
     * @return
     */
    @RequestMapping(value = "/shoppingCart")
    public APIResponse<?> shoppingCart(@RequestBody ReqShoppingCart reqShoppingCart){
        try {
            return placeOrderService.shoppingCart(reqShoppingCart);
        } catch (Exception e) {
            e.printStackTrace();
            return APIResponse.returnFail(ObjectUtil.getFullStackTraceToString(e));
        }
    }

    /**
     * 商家端购物车预订再支付
     * @param map
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/shoppingCartBookPay")
    public APIResponse<?> shoppingCart(@RequestBody @Valid Map<String,Object> map, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        String reqPlaceOrderId = MapUtils.getString(map, "reqPlaceOrderId");
        Integer adminPayType = MapUtils.getInteger(map, "adminPayType");
        String receivedAmount = MapUtils.getString(map, "receivedAmount");
        String memo = MapUtils.getString(map,"memo");
        String expression = MapUtils.getString(map,"expression");
        String scanCode = MapUtils.getString(map,"scanCode");
        if (StringUtils.isEmpty(reqPlaceOrderId)){
            return APIResponse.returnFail("没有指定OrderId");
        }
        if (StringUtils.isEmpty(adminPayType)){
            return APIResponse.returnFail("请选择商家端支付方式");
        }
        if (StringUtils.isEmpty(receivedAmount)){
            return APIResponse.returnFail("实付金额不能为空");
        }
        if (StringUtils.isEmpty(expression)){
            expression = "";
        }

        boolean isScanPayFlag = false;
        if(MerchantClientPayType.getMatchScanPayType().containsKey(adminPayType.toString())){
            isScanPayFlag = true;
            if(!PlaceConstants.PLACE_ADMIN_PAY_WAYS_PO_UNIONPAY.equals(adminPayType.toString())){//扫码支付方式匹配 update by tim
                if (StringUtils.isEmpty(scanCode)){//微信、支付宝必须输入支付码
                    return APIResponse.returnFail("支付失败-支付码必需");
                }
            }
        }

        try {
            if (isScanPayFlag){
                TbBizPlaceOrder tbBizPlaceOrder = placeOrderService.getTbBizPlaceOrderByOrderNoForShoppingCart(reqPlaceOrderId,adminPayType,receivedAmount,memo,expression);
                PlaceScanOrderPayVo placeScanOrderPayVo = new PlaceScanOrderPayVo();
                placeScanOrderPayVo.setReceivedAmount(new BigDecimal(receivedAmount));
                placeScanOrderPayVo.setAdminPayType(adminPayType.toString());
                placeScanOrderPayVo.setScanCode(scanCode);
                placeScanOrderPayVo.setTbBizPlaceOrder(tbBizPlaceOrder);
                placeScanOrderPayVo.setTbBizPlacePrivateOrder(null);
                return placeOrderService.scanOrderPay(placeScanOrderPayVo);
            } else {
                return placeOrderService.shoppingCartBookPay(reqPlaceOrderId, adminPayType,
                        receivedAmount, memo, expression, scanCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return APIResponse.returnFail(/*ObjectUtil.getFullStackTraceToString(e)*/e.getMessage());
        }
    }

    /**
     * 校验购物车会员卡是否支持
     * @param reqShoppingCart
     * @return
     */
    @RequestMapping(value = "/checkMemberCard")
    public APIResponse<?> checkMemberCard(@RequestBody ReqShoppingCart reqShoppingCart){
        try {
            return placeOrderService.checkMemberCard(reqShoppingCart);
        } catch (Exception e) {
            e.printStackTrace();
            return APIResponse.returnFail(ObjectUtil.getFullStackTraceToString(e));
        }
    }

    /**
     * 扫码获取商品信息
     * @param map
     * @return
     */
    @RequestMapping(value = "/scanGoodsInfo")
    public APIResponse<?> scanGoodsInfo(@RequestBody @Valid Map<String, Object> map,
                                        BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer placeId = MapUtils.getInteger(map, "placeId");
        String goodsBarCode = MapUtils.getString(map,"goodsBarCode");
        if (placeId == null){
            return APIResponse.returnFail("placeId must input");
        }
        if (goodsBarCode == null){
            return APIResponse.returnFail("goodsBarCode must input");
        }
        return placeSaleService.getGoodsInfoByBarCode(placeId,goodsBarCode);
    }

    /**
     * 商品快速加入购物车校验
     * @param reqPlaceGoodsVo
     * @return
     */
    @RequestMapping(value = "/checkGoodCartForGoods")
    public APIResponse<?> checkGoodCartForGoods(@RequestBody ReqPlaceGoodsVo reqPlaceGoodsVo){
        return placeOrderService.checkGoodCartForGoods(reqPlaceGoodsVo);
    }

    /**
     * 购物车获取会员卡列表
     * @param map
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/shoppingCardGetMemberCardList",method = RequestMethod.POST)
    public APIResponse<?> shoppingCardGetMemberCardList(@RequestBody @Valid Map<String,Object> map, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        String userName = MapUtils.getString(map, "userName");
        String phone = MapUtils.getString(map, "phone");
        Integer placeId = MapUtils.getInteger(map, "placeId");
//        Integer categoryId = MapUtils.getInteger(map, "categoryId");
//        if (StringUtils.isEmpty(userName)){
//            return APIResponse.returnFail("姓名为空");
//        }

        if (StringUtils.isEmpty(phone)){
            return APIResponse.returnFail("手机号为空");
        }
        if (StringUtils.isEmpty(placeId)){
            return APIResponse.returnFail("场馆编号不能为空");
        }
//        return placeMemberService.shoppingCardGetMemberCardList(userName,phone,placeId,categoryId);
        return placeMemberService.shoppingCardGetMemberCardList(userName,phone,placeId);
    }


    /**
     * 补充散客信息-姓名
     * @param map
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/supplementFITName")
    public APIResponse<?> supplementFITName(@RequestBody @Valid Map<String,Object> map, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        String customerName = MapUtils.getString(map,"customerName");
        String placeOrderId = MapUtils.getString(map,"orderNo");
        return placeSaleService.supplementFITName(customerName,placeOrderId);
    }

    /**
     * 补充散客信息-电话
     * @param map
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/supplementFITMobile")
    public APIResponse<?> supplementFITMobile(@RequestBody @Valid Map<String,Object> map, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        String mobile = MapUtils.getString(map,"mobile");
        String placeOrderId = MapUtils.getString(map,"orderNo");
        return placeSaleService.supplementFITMobile(mobile,placeOrderId);
    }

}

